package cn.myspringboot.system.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.system.service.PermissionService;
import cn.myspringboot.user.domain.Permission;
import cn.myspringboot.user.query.PermissionQuery;
import cn.myspringboot.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api("权限修改的接口")
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @PreAuthorize(name = "添加或修改权限信息",sn = "permission:insert",descs ="添加或修改权限信息")
    @ApiOperation("添加或修改权限信息")
    @PostMapping
    public R insert(@RequestBody Permission permission) {
        if(permission.getId() == null){
            permissionService.add(permission);
        }else {
            permissionService.update(permission);
        }
        return R.ok();
    }
    @PreAuthorize(name = "根据id删除权限信息",sn = "permission:delete",descs ="根据id删除权限信息")
    @ApiOperation("根据id删除权限信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        permissionService.delete(id);
        return R.ok();
    }
    @PreAuthorize(name = "批量删除权限信息",sn = "permission:delBatch",descs ="批量删除权限信息")
    @ApiOperation("批量删除权限信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        permissionService.batchDelete(ids);
        return R.ok();
    }
    @PreAuthorize(name = "根据id查询权限信息",sn = "permission:getById",descs ="根据id查询权限信息")
    @ApiOperation("根据id查询权限信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(permissionService.getById(id));
    }
    @PreAuthorize(name = "查询所有权限信息",sn = "permission:getAll",descs ="查询所有权限信息")
    @ApiOperation("查询所有权限信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(permissionService.getAll());
    }
    @PreAuthorize(name = "分页查询权限信息",sn = "permission:page",descs ="分页查询权限信息")
    @ApiOperation("分页查询权限信息")
    @PostMapping("/page")
    public R page(@RequestBody PermissionQuery permissionQuery) {
        return R.ok(permissionService.queryPage(permissionQuery));
    }
}
